def is_prime(num):
    for div in range(2, num):
        if num % div == 0:
            return False
    return True


number = int(input())
if 1_000 <= number < 10_000:
    if is_prime(number):
        print(number)
    else:
        d = [number // 1000, number // 100 % 10, number // 10 % 10, number % 10]
        max_value = 0
        for d1 in range(0, 2):
            for d2 in range(d1 + 1, 3):
                for d3 in range(d2 + 1, 4):
                    num = int(str(d[d1]) + str(d[d2]) + str(d[d3]))
                    if is_prime(num):
                        max_value = max(max_value, num)
        for d1 in range(0, 3):
            for d2 in range(d1 + 1, 4):
                num = int(str(d[d1]) + str(d[d2]))
                if is_prime(num):
                    max_value = max(max_value, num)
        for d1 in d:
            if is_prime(d1):
                max_value = max(max_value, d1)
        print(max_value)
else:
    print(0)

